package ph.com.gs3.formalistics.service.services;

import java.util.List;

import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import ph.com.gs3.formalistics.model.communicators.CommentCommunicatorModule;
import ph.com.gs3.formalistics.model.communicators.HttpCommunicator;
import ph.com.gs3.formalistics.model.dao.CommentsDAO;
import ph.com.gs3.formalistics.model.dao.UserAccountsDAO;
import ph.com.gs3.formalistics.model.valueobjects.business.Comment;
import ph.com.gs3.formalistics.model.valueobjects.business.User;
import ph.com.gs3.formalistics.service.managers.CommentManager;
import ph.com.gs3.formalistics.service.managers.CommentManager.CommentDeleteFromServerFailedException;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;

public class DeleteCommentsMarkedForDeletionService extends IntentService {

	// constants
	public static final String TAG = DeleteCommentsMarkedForDeletionService.class.getSimpleName();
	public static final String NAME = DeleteCommentsMarkedForDeletionService.class.getName();

	public static final String ACTION_ON_SERVICE_STARTED = NAME + "_on_service_started";
	public static final String ACTION_ON_SERVICE_DONE = NAME + "_on-service_done";

	public static final String EXTRA_CURRENT_USER = "current_user";

	// dependencies
	private UserAccountsDAO userAccountsDAO;
	private CommentsDAO commentsDAO;
	private CommentCommunicatorModule commentCommunicatorModule;
	private CommentManager commentManager;

	// fields
	private User currentUser;

	public DeleteCommentsMarkedForDeletionService() {
		super(TAG);
	}

	@Override
	protected void onHandleIntent(Intent intent) {

		intializeFieldsFromExtras(intent.getExtras());
		initializeDependencies();

		broadcastServiceStarted();

		String server = currentUser.getAccountServer();
		String userWebId = Integer.toString(currentUser.getWebId());

		// get all comments marked for deletion
		commentsDAO.open();
		List<Comment> commentsForDeletion = commentsDAO.getAllCommentsMarkedForDeletion(server,
		        userWebId);
		commentsDAO.close();

		for (Comment comment : commentsForDeletion) {

			try {
				FLLogger.d(TAG, "Deleting comment: " + comment.toString());
				commentManager.deleteCommentMarkedForDeletion(comment);
			} catch (CommentDeleteFromServerFailedException e) {
				e.printStackTrace();
			}

		}

		broadcastServiceDone();
	}

	private void intializeFieldsFromExtras(Bundle extras) {

		currentUser = (User) extras.getSerializable(EXTRA_CURRENT_USER);

	}

	private void initializeDependencies() {

		String server = currentUser.getAccountServer();

		HttpCommunicator communicator = new HttpCommunicator();
		commentCommunicatorModule = new CommentCommunicatorModule(communicator, server);

		userAccountsDAO = new UserAccountsDAO(getApplicationContext());
		commentsDAO = new CommentsDAO(getApplicationContext());

		commentManager = new CommentManager(userAccountsDAO, commentsDAO, commentCommunicatorModule);

	}

	private void broadcastServiceStarted() {

		Intent intent = new Intent();
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		intent.setAction(ACTION_ON_SERVICE_STARTED);
		sendBroadcast(intent);

	}

	private void broadcastServiceDone() {

		Intent intent = new Intent();
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		intent.setAction(ACTION_ON_SERVICE_DONE);
		sendBroadcast(intent);

	}

}
